package com.haircut.sys.service.permission.impl;

import cn.hutool.core.util.ObjectUtil;
import com.haircut.sys.dao.dept.SyDeptMapper;
import com.haircut.sys.dao.role.SysRoleMapper;
import com.haircut.sys.model.dept.SysDept;
import com.haircut.sys.model.role.SysRole;
import com.haircut.sys.service.permission.IPermissionService;
import com.haircut.sys.utils.user.SecurityUtils;
import com.haircut.sys.vo.permission.PermissionVo;
import com.haircut.sys.vo.user.LoginUserVo;
import com.haircut.utils.PermissionUtils;
import com.modular.enums.DataScope;
import com.modular.enums.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private SyDeptMapper deptMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<String> findPermissionSqlList(String parentUserId,String parentDeptId)
    {
        LoginUserVo userVo = SecurityUtils.getLoginUser();
        SysRole role = sysRoleMapper.selectById(userVo.getRoleId());
        SysDept dept = deptMapper.selectById(userVo.getDeptId());
        PermissionVo permissionVo = new PermissionVo();
        permissionVo.setDataScope(role.getDataScope());
        permissionVo.setDeptId(userVo.getDeptId());
        permissionVo.setUserId(userVo.getUserId());
        permissionVo.setHierarchy(dept.getHierarchy());
        permissionVo.setParentUserId(parentUserId);
        permissionVo.setParentDeptId(parentDeptId);
        return PermissionUtils.coverPermission(permissionVo);
    }

    /**
     * 获得权限sql

     * @param parentDeptId
     * @return
     */
    @Override
    public List<String> findPermissionDeptSqlList(String parentDeptId)
    {
        LoginUserVo userVo = SecurityUtils.getLoginUser();
        SysRole role = sysRoleMapper.selectById(userVo.getRoleId());
        // 如果是系统管理员，则
        if(ObjectUtil.equal(role.getDataSource(), DataSource.SYS.getCode())){
            return null;
        }
        SysDept dept = deptMapper.selectById(userVo.getDeptId());
        Integer dataScope = role.getDataScope();
        if(ObjectUtil.equal(role.getDataScope(), DataScope.SELF.getCode())){
            dataScope = DataScope.DEPT.getCode();
        }
        PermissionVo permissionVo = new PermissionVo();
        permissionVo.setDataScope(dataScope);
        permissionVo.setDeptId(userVo.getDeptId());
        permissionVo.setUserId(userVo.getUserId());
        permissionVo.setHierarchy(dept.getHierarchy());
        permissionVo.setParentDeptId(parentDeptId);
        return PermissionUtils.coverPermission(permissionVo);

    }

    /**
     * 获得权限sql

     * @param parentDeptId
     * @return
     */
    @Override
    public List<String> findPermissionStaticSqlList(String parentUserId,String parentDeptId)
    {
        LoginUserVo userVo = SecurityUtils.getLoginUser();
        SysRole role = sysRoleMapper.selectById(userVo.getRoleId());
        Integer dataSource = role.getDataSource();
        // 如果是系统管理员，则
        if(ObjectUtil.equal(role.getDataSource(), DataSource.SYS.getCode())){
            dataSource = DataScope.DEPT.getCode();
        }
        if(ObjectUtil.equal(role.getDataScope(),DataScope.DEPT_LEVL.getCode())){
            dataSource = DataScope.DEPT.getCode();
        }
        SysDept dept = deptMapper.selectById(userVo.getDeptId());
        PermissionVo permissionVo = new PermissionVo();
        permissionVo.setDataScope(dataSource);
        permissionVo.setDeptId(userVo.getDeptId());
        permissionVo.setUserId(userVo.getUserId());
        permissionVo.setHierarchy(dept.getHierarchy());
        permissionVo.setParentUserId(parentUserId);
        permissionVo.setParentDeptId(parentDeptId);
        return PermissionUtils.coverPermission(permissionVo);

    }
}
